<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/murmur/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/murmur/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/murmur/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/murmur/images/logo.svg" color="#222">

<link rel="stylesheet" href="/murmur/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.css">
  <script src="//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.js"></script>

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gitee.com","root":"/murmur/","scheme":"Gemini","version":"8.0.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"disqus","storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}};
  </script>

  <meta name="description" content="讲解Java面向对象继承的用法，了解对象转型；区分overraide重写和overload重载的区别；了解基于final关键字修饰的终极类和终极成员；了解abstract抽象类与接口的区别；">
<meta property="og:type" content="article">
<meta property="og:title" content="计应本的自考笔记(6)-Java继承与多态">
<meta property="og:url" content="https://gitee.com/lianghuilin/murmur.git/university_notes/Java(6)-%E7%BB%A7%E6%89%BF%E4%B8%8E%E5%A4%9A%E6%80%81/index.html">
<meta property="og:site_name" content="汇话板">
<meta property="og:description" content="讲解Java面向对象继承的用法，了解对象转型；区分overraide重写和overload重载的区别；了解基于final关键字修饰的终极类和终极成员；了解abstract抽象类与接口的区别；">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/lianghuilin/blog-image-warehouse/raw/master/20220327192720.png">
<meta property="article:published_time" content="2022-03-27T14:22:12.000Z">
<meta property="article:modified_time" content="2022-12-15T13:00:13.148Z">
<meta property="article:author" content="William Leung">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/lianghuilin/blog-image-warehouse/raw/master/20220327192720.png">


<link rel="canonical" href="https://gitee.com/lianghuilin/murmur.git/university_notes/Java(6)-%E7%BB%A7%E6%89%BF%E4%B8%8E%E5%A4%9A%E6%80%81/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>计应本的自考笔记(6)-Java继承与多态 | 汇话板</title>
  


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?a49081c570a782a242cdf6e19e1a584d";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/murmur/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">汇话板</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">设定目标，放慢步调，用温和的力量自我改革</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/murmur/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/murmur/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <section class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AF%B9%E8%B1%A1%E7%BB%A7%E6%89%BF%E5%92%8C%E2%80%9Cis-a%E2%80%9D%E5%85%B3%E7%B3%BB"><span class="nav-number">1.</span> <span class="nav-text">对象继承和“is a”关系</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#extends%E5%85%B3%E9%94%AE%E5%AD%97"><span class="nav-number">2.</span> <span class="nav-text">extends关键字</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Object%E7%B1%BB"><span class="nav-number">3.</span> <span class="nav-text">Object类</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8D%95%E7%BB%A7%E6%89%BF%E7%89%B9%E6%80%A7"><span class="nav-number">4.</span> <span class="nav-text">单继承特性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AF%B9%E8%B1%A1%E8%BD%AC%E5%9E%8B"><span class="nav-number">5.</span> <span class="nav-text">对象转型</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AD%90%E7%B1%BB%E5%92%8C%E7%88%B6%E7%B1%BB%E7%9A%84%E8%BD%AC%E5%9E%8B%E8%A7%84%E5%88%99"><span class="nav-number">5.1.</span> <span class="nav-text">子类和父类的转型规则</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#instanceof"><span class="nav-number">5.2.</span> <span class="nav-text">instanceof</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Override-%E6%96%B9%E6%B3%95%E8%A6%86%E7%9B%96"><span class="nav-number">6.</span> <span class="nav-text">Override(方法覆盖)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Override%E7%9A%84%E8%A7%84%E5%88%99"><span class="nav-number">6.1.</span> <span class="nav-text">Override的规则</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Override%E4%B8%8EOverload%E5%8C%BA%E5%88%AB"><span class="nav-number">6.2.</span> <span class="nav-text">Override与Overload区别</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#super"><span class="nav-number">6.3.</span> <span class="nav-text">super</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%90%86%E8%A7%A3%E5%A4%9A%E6%80%81"><span class="nav-number">6.4.</span> <span class="nav-text">理解多态</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#final%E7%BB%88%E6%9E%81%E7%B1%BB%E3%80%81%E6%96%B9%E6%B3%95%E5%92%8C%E5%B1%9E%E6%80%A7"><span class="nav-number">7.</span> <span class="nav-text">final终极类、方法和属性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Abstract%E6%8A%BD%E8%B1%A1%E7%B1%BB"><span class="nav-number">8.</span> <span class="nav-text">Abstract抽象类</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Interface%E6%8E%A5%E5%8F%A3"><span class="nav-number">9.</span> <span class="nav-text">Interface接口</span></a></li></ol></div>
        </section>
        <!--/noindex-->

        <section class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">William Leung</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/murmur/archives">
          <span class="site-state-item-count">43</span>
          <span class="site-state-item-name">文章</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/murmur/categories/">
          
        <span class="site-state-item-count">9</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/murmur/tags/">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </section>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">
      

      

  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/lianghuilin/murmur.git/university_notes/Java(6)-%E7%BB%A7%E6%89%BF%E4%B8%8E%E5%A4%9A%E6%80%81/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/murmur/images/avatar.gif">
      <meta itemprop="name" content="William Leung">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="汇话板">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          计应本的自考笔记(6)-Java继承与多态
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-03-27 22:22:12" itemprop="dateCreated datePublished" datetime="2022-03-27T22:22:12+08:00">2022-03-27</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-12-15 21:00:13" itemprop="dateModified" datetime="2022-12-15T21:00:13+08:00">2022-12-15</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/murmur/categories/%E8%AE%A1%E5%BA%94%E6%9C%AC%E7%9A%84%E8%87%AA%E8%80%83%E7%AC%94%E8%AE%B0/" itemprop="url" rel="index"><span itemprop="name">计应本的自考笔记</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>3.7k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>3 分钟</span>
    </span>
</div>

            <div class="post-description">讲解Java面向对象继承的用法，了解对象转型；区分overraide重写和overload重载的区别；了解基于final关键字修饰的终极类和终极成员；了解abstract抽象类与接口的区别；</div>
        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="对象继承和“is-a”关系"><a href="#对象继承和“is-a”关系" class="headerlink" title="对象继承和“is a”关系"></a>对象继承和“is a”关系</h2><p>就像“万物皆对象”口头禅说的一样，Java中类层次的树形结构，跟日常我们理解和描述大千世界是一样的。我们会把动物，先划分为脊椎动物、节肢动物和软体动物。然后在脊椎动物里面又分哺乳动物，哺乳动物又可以细分猫和狗。</p>
<p><img src="https://gitee.com/lianghuilin/blog-image-warehouse/raw/master/20220327192720.png"></p>
<p>对于动物的描述，从大到小，由宽泛到具体。大的更一般的类看作是父类。包含在其中的，具体的类是子类。</p>
<p>子类与父类的关系是：子类对象“is kind of/is a” 父类对象。这也就是面向对象的继承特性，通过继承，可以增加软件的可复用性，使得代码在类之间共享。</p>
<blockquote>
<p>注意：“is a”与“has a”关系是不同的。比如狗，是动物的一种，也可以说动物中包含了狗。但发动机属于汽车，却不能说发动机是汽车的一种。</p>
</blockquote>
<h2 id="extends关键字"><a href="#extends关键字" class="headerlink" title="extends关键字"></a>extends关键字</h2><p>先统一下名词：继承/派生是面向对象的特性之一。子类继承自父类，子类也称为派生类，父类也称为基类或者超类。</p>
<p>比如我们现在要实现一个Employee(员工)类，一个Manager(管理)类。普通员工拥有员工编号、姓名、职称、职级、生日等属性，管理则是特别的员工，额外拥有管理组的属性，以及管理的若干个员工。</p>
<ul>
<li>员工类</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Employee</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String name, jobTitle;</span><br><span class="line">    <span class="keyword">private</span> Date birthday;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> jobGrade;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>管理类</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Manager</span> <span class="keyword">extends</span> <span class="title">Employee</span></span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String group;</span><br><span class="line">    <span class="keyword">private</span> Employee[] subordinates;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>由于Manager继承自Employee，也就拥有了普通员工的所有成员。增加了可维护性，子类是更个性化的存在，子类的改动也不会牵动到父类。</p>
<h2 id="Object类"><a href="#Object类" class="headerlink" title="Object类"></a>Object类</h2><p>Object类是Java中所有类的直接或间接父类，处于类层次的最高点。</p>
<p>Object类提供了比如<code>getClass()</code>、<code>toString()</code>以及<code>equals()</code>等基础方法。</p>
<p>关于判定对象是否相等有两种方式，一种是==运算符，判定对象引用是否相等；另一种是使用<code>equals()</code>方法，判定对象的类型，对象属性值是否相等。</p>
<h2 id="单继承特性"><a href="#单继承特性" class="headerlink" title="单继承特性"></a>单继承特性</h2><p>上面提到，子类用extends关键字来继承父类，那能不能继承多个父类呢？答案是不行的。</p>
<p>由于多重继承是网状的关系，子类和父类都是多对多的关系，而子类的多个父类一旦有同名的属性或者方法，很容易造成子类实例的混乱。而单继承关系则是树状结构，如果需要多继承能力则可以通过接口来实现。</p>
<blockquote>
<p>注意：子类可以继承所能继承的方法和成员变量，但不能继承构造函数。</p>
</blockquote>
<h2 id="对象转型"><a href="#对象转型" class="headerlink" title="对象转型"></a>对象转型</h2><h3 id="子类和父类的转型规则"><a href="#子类和父类的转型规则" class="headerlink" title="子类和父类的转型规则"></a>子类和父类的转型规则</h3><p><code>Employee e = new Manager()</code>，将子类的实例赋值给父类变量，这称为“对象转型(Casting)”。</p>
<p>变量可以赋值本类的实例，也可以只想子类的实例，这就是对象多态性的体现，但反过来不行。</p>
<p>对象引用转型的规则：</p>
<ul>
<li>沿着类层次向“上”转型总是合法的。比如Manager子类，转成Employee父类；</li>
<li>向“下”转型，只能是沿着继承链的父类转子类，其他类是不允许的，且要使用显式转换；</li>
</ul>
<h3 id="instanceof"><a href="#instanceof" class="headerlink" title="instanceof"></a>instanceof</h3><p>这时可能你会想了，变量e可以赋值Employee的实例对象，可以赋值Manager子类实例对象，我怎么知道具体是什么对象呢？我要知道是子类对象，才能访问它特有的属性和方法啊？答案就是<code>instanceof</code>运算符</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">System.out.println(e <span class="keyword">instanceof</span> Manager); <span class="comment">// true</span></span><br></pre></td></tr></table></figure>

<h2 id="Override-方法覆盖"><a href="#Override-方法覆盖" class="headerlink" title="Override(方法覆盖)"></a>Override(方法覆盖)</h2><p>当父类的方法不能满足需求时，子类可以修改父类提供的方法，这称为方法覆盖/方法重写(Override)。这也是面向对象多态的特性。</p>
<h3 id="Override的规则"><a href="#Override的规则" class="headerlink" title="Override的规则"></a>Override的规则</h3><ul>
<li>子类重写父类的方法，要使用相同的方法名以及参数列表。</li>
<li>同时子类方法不能比父类方法的访问权限更严格，比如，弗雷方法的访问权限是public，那么子类方法则不能是private的修饰。还有。</li>
<li>子类方法抛出的异常也不能比原方法多。</li>
</ul>
<h3 id="Override与Overload区别"><a href="#Override与Overload区别" class="headerlink" title="Override与Overload区别"></a>Override与Overload区别</h3><p>如果方法名相同，但参数列表不同，则是Overload(方法重载)。Overload是发生在同一个类，而Override则发生在父类子类中。</p>
<h3 id="super"><a href="#super" class="headerlink" title="super"></a>super</h3><p>如果子类想使用父类被隐藏的方法，可以通过super指针访问。调用<code>super()</code>可显式的调用父类的构造函数。</p>
<h3 id="理解多态"><a href="#理解多态" class="headerlink" title="理解多态"></a>理解多态</h3><p>多态，是面向对象语言的重要特性。Java规定了，多态的情况下，执行的是对象真实的类型(运行时类型)的方法，而不是声明类型(静态类型)的方法。</p>
<p>这里解释一下静态类型和动态类型：</p>
<ul>
<li>对象声明时称为静态类型，或者说引用类型。我们编写的代码编译时就确定下来的类型；</li>
<li>而代码在运行过程中，指向的对象类型，称为动态类型，这是真正的类型；</li>
</ul>
<h2 id="final终极类、方法和属性"><a href="#final终极类、方法和属性" class="headerlink" title="final终极类、方法和属性"></a>final终极类、方法和属性</h2><p>关键字<code>final</code>，既可以修饰类，也可以修饰类当中的属性和方法，被final修饰表示不能改变。final类不能被继承，不再拥有子类。final方法不能被重写；final变量，值不能被改变。</p>
<p>怎么会有要求不能被继承的类呢？有的，像<code>Java.lang.String</code>类就是如此，类的结构和功能都很完美了，用就完事了，你不用再扩展和补充。这样做的目的，是为了保证程序中，如果有指向String类的引用，那它就是真的String类型，而不是被改过的String子类。</p>
<p>final方法也是如此，同时final方法还利于优化，提高编译运行的效率。</p>
<p>通过final定义常量，应该是Java程序员最熟悉的操作了。值得注意的是，如果该常量是引用类型，那么不能再篡改引用地址，但可以对原对象的属性值的修改，或者进行扩展属性。</p>
<h2 id="Abstract抽象类"><a href="#Abstract抽象类" class="headerlink" title="Abstract抽象类"></a>Abstract抽象类</h2><p>设计程序的时候，需要创建某个类仅仅定义基本行为和属性，但又不宜在这个类就进行具体的实现，而是希望等子类根据实际情况再去实现这些方法。这种定义了方法，但没有方法的具体实现的类，称为抽象类，通过abstract关键字定义。抽象类中通过abstract定义的方法，称为抽象方法。</p>
<p>抽象类是用来进行继承的，所以不能直接实例化。</p>
<p>抽象类可以包含常规类能包含的任何成员方法，包括构造函数，通常也会包含抽象方法，但反过来常规类中不能定义抽象方法。</p>
<ul>
<li>抽象类Storage</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">Storage</span> </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> objectNum = <span class="number">0</span>;  <span class="comment">// 存储了多少个对象</span></span><br><span class="line">    Object storage[] = <span class="keyword">new</span> Object[<span class="number">10</span>];  <span class="comment">// 存储容器</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">put</span><span class="params">(Object obj)</span></span>;  <span class="comment">// 存入方法</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> Object <span class="title">get</span><span class="params">()</span></span>;  <span class="comment">// 获取方法</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>常规类Stack</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 实现栈结构</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Stack</span> <span class="keyword">extends</span> <span class="title">Storage</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> point = <span class="number">0</span>; <span class="comment">// 指针</span></span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">put</span><span class="params">(Object obj)</span> </span>&#123;</span><br><span class="line">        storage[point++] = obj;</span><br><span class="line">        objectNum++;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function">Object <span class="title">get</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        objectNum--;</span><br><span class="line">        <span class="keyword">return</span> storage[point];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>常规类Queue</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 实现队列结构</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Queue</span> <span class="keyword">extends</span> <span class="title">Storage</span></span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> top = <span class="number">0</span>, bottom = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">put</span><span class="params">(Object obj)</span> </span>&#123;</span><br><span class="line">        storage[top++] = obj;</span><br><span class="line">        objectNum++;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function">Object <span class="title">get</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        objectNum--;</span><br><span class="line">        <span class="keyword">return</span> storage[bottom++];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Interface接口"><a href="#Interface接口" class="headerlink" title="Interface接口"></a>Interface接口</h2><p>接口，是特殊的抽象类，可以看作是更纯粹的抽象类。它只规定了类的基本形式，接口中可以有成员变量，但系统会默认加上final和static关键字来修饰，所以必须对成员变量赋初始值，接口内的方法必须是抽象方法。</p>
<p>接口可以实现多继承特性，一个子类只能继承一个父类，但可以实现多个接口，使得代码具有更清晰的结构。</p>
<ul>
<li>CharStorage接口</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">CharStorage</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">put</span><span class="params">(<span class="keyword">char</span> c)</span></span>;</span><br><span class="line">    <span class="function"><span class="keyword">char</span> <span class="title">get</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>CharStack类</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CharStack</span> <span class="keyword">implements</span> <span class="title">CharStorage</span></span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">char</span> member[] = <span class="keyword">new</span> <span class="keyword">char</span>[<span class="number">10</span>];</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> point = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">put</span><span class="params">(<span class="keyword">char</span> c)</span> </span>&#123;</span><br><span class="line">        member[point] = c;</span><br><span class="line">        point++;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">char</span> <span class="title">get</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        point--;</span><br><span class="line">        <span class="keyword">return</span> member[point];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>William Leung
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://gitee.com/lianghuilin/murmur.git/university_notes/Java(6)-%E7%BB%A7%E6%89%BF%E4%B8%8E%E5%A4%9A%E6%80%81/" title="计应本的自考笔记(6)-Java继承与多态">https://gitee.com/lianghuilin/murmur.git/university_notes/Java(6)-继承与多态/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/murmur/university_notes/Java(5)-%E6%95%B0%E7%BB%84%E3%80%81%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%8E%E5%90%91%E9%87%8F/" rel="prev" title="计应本的自考笔记(5)-Java数组、字符串与向量">
                  <i class="fa fa-chevron-left"></i> 计应本的自考笔记(5)-Java数组、字符串与向量
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/murmur/crush_notes/CSS%20Grid%E5%AE%9E%E7%8E%B0%E6%8E%92%E8%A1%8C%E6%A6%9C%EF%BC%8C%E6%8E%A7%E5%88%B6%E7%BD%91%E6%A0%BC%E7%9A%84%E6%B5%81%E5%8A%A8%E6%96%B9%E5%90%91/" rel="next" title="CSS Grid实现排行榜，控制网格的流动方向">
                  CSS Grid实现排行榜，控制网格的流动方向 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






      

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

    </div>
  </main>

  <footer class="footer">
    <div class="footer-inner">
      

      

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">William Leung</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">142k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">2:09</span>
  </span>
</div>
<div class="busuanzi-count">
    <span id="busuanzi_container_site_uv">
      本站访客数<span id="busuanzi_value_site_uv"></span>人次
    </span>
    <span id="busuanzi_container_site_pv">
        本站总访问量<span id="busuanzi_value_site_pv"></span>次
    </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/murmur/js/utils.js"></script><script src="/murmur/js/motion.js"></script><script src="/murmur/js/next-boot.js"></script>

  
















  <script>
    NProgress.configure({
      showSpinner: true
    });
    NProgress.start();
    document.addEventListener('readystatechange', () => {
      if (document.readyState === 'interactive') {
        NProgress.inc(0.8);
      }
      if (document.readyState === 'complete') {
        NProgress.done();
      }
    });
    document.addEventListener('pjax:send', () => {
      NProgress.start();
    });
    document.addEventListener('pjax:success', () => {
      NProgress.done();
    });
  </script>


  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>








  

  

</body>
</html>
